package com.hpwclass.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hpwclass.entity.YdlLoginUser;
import com.hpwclass.entity.YdlUser;
import com.hpwclass.service.YdlUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huangpw
 * @date 2024/3/19 16:44
 * 博客： https://blog.csdn.net/qq_18167779?type=blog
 * @description:
 */

@RestController
@Slf4j
public class IndexController {

    @Resource
    private YdlUserService userService;

    @PostMapping("login")
    public ResponseEntity<YdlLoginUser> login(@RequestBody @Validated YdlUser ydlUser, BindingResult bindingResult) {
        // 1、处理不合法的数据
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        allErrors.forEach( error -> log.error("登陆时用户校验失败：{}",error.getDefaultMessage()));
        if(allErrors.size() > 0) {
            return ResponseEntity.status(500).build();
        }

        // 2、执行登录逻辑
        YdlLoginUser ydlLoginUser = null;
        try {
            ydlLoginUser = userService.login(ydlUser.getUserName(), ydlUser.getPassword());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok().body(ydlLoginUser);
    }

    @GetMapping("logout")
    public ResponseEntity<String> logout() {
        try {
           userService.logout();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok().body("退出成功");
    }
}
